package com.ktjy.service.impl;

import com.ktjy.entity.Supplier;
import com.ktjy.mapper.SupplierMapper;
import com.ktjy.service.SupplierService;
import com.ktjy.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version v1.0
 * @Author JieGe
 * @Create 2024-12-07 - 11:07
 */
// 等价于： <bean id="supplierService" class="com.ktjy.service.impl.SupplierServiceImpl">
@Service("supplierService")
public class SupplierServiceImpl implements SupplierService {

    @Autowired // 按接口类型去Spring容器中找其依赖对象，并注入给下面属性
    private SupplierMapper supplierMapper;

    /*public void setSupplierMapper(SupplierMapper supplierMapper) {
        this.supplierMapper = supplierMapper;
    }*/

    @Override
    public List<Supplier> getSuppliersBySupName(String supName) {
        return supplierMapper.getSuppliersBySupName(supName);
    }

    // 批量新增多个供货商信息
    @Override
    public int saveBatch(List<Supplier> suppliers) {
        int rows = 0;
        // 使用for循环，批量新增多个供货商信息
        for (int i = 0; i < suppliers.size(); i++) {
            Supplier supplier = suppliers.get(i);
            // 调用addSupplier方法新增供货商信息
            rows += supplierMapper.addSupplier(supplier);
        }
        // 返回受影响的总行数
        return rows;
    }

    @Override
    public int delSupplier(int id) {
        return supplierMapper.delSupplier(id);
    }

    @Override
    public int updateSupplier(Supplier supplier) {
        return supplierMapper.updateSupplier(supplier);
    }

    /**
     * 按条件(按供应商编码和供应商名称模糊查询)查询，并分页显示供应商列表
     * @param entity 供应商对象
     * @param pageNo 当前页码
     * @param pageSize 每页显示的数据行数
     */
    @Override
    public Page<Supplier> getSuppliersPage(Supplier entity, Integer pageNo, Integer pageSize) {

        Page<Supplier> page = null;
        try {
            //1、实例化Page对象
            page = new Page<Supplier>();
            // 2、为page对象的属性赋值
            page.setPageNo(pageNo);  // 当前页码
            page.setPageSize(pageSize);
            Long count = supplierMapper.count(entity);  // 查询总记录数
            page.setTotalCount(count.intValue());
            // 查询供应商分页数据
            List<Supplier> data = supplierMapper.getSuppliersPage(entity, page.offset(), page.getPageSize());
            page.setData(data);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    // 新增或修改供货商信息
    @Override
    public boolean saveOrUpdate(Supplier supplier) {
        try {
            if (supplier.getId() == null) {
                // 供货商的id为空，就调用新增方法，实现新增的功能
                return supplierMapper.addSupplier(supplier) > 0;
            } else {
                // 说明id不为空，调用修改方法，实现修改的功能
                return supplierMapper.updateSupplier(supplier) > 0;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // 通过供货商id查询供货商详情信息
    @Override
    public Supplier getSupplier(Integer id) {
        Supplier supplier = null;
        try {
            // 查询供货商详情
            supplier = supplierMapper.getSupplier(id);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return supplier;
    }
}
